

package com.cloud.yanglaocloud.controller;

import com.cloud.yanglaocloud.form.RegisterForm;
import com.cloud.yanglaocloud.utils.R;
import com.cloud.yanglaocloud.entity.UserEntity;
import com.cloud.yanglaocloud.form.LoginForm;
import com.cloud.yanglaocloud.service.CaptchaService;
import com.cloud.yanglaocloud.service.UserService;
import com.cloud.yanglaocloud.service.UserTokenService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.spec.ECField;
import java.util.Random;
import java.security.SecureRandom;
/**
 * 登录相关
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
public class LoginController extends AbstractController {
    @Autowired
    private UserService UserService;
    @Autowired
    private UserTokenService UserTokenService;
    @Autowired
    private CaptchaService CaptchaService;

    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    public void captcha(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam("uuid") String uuid) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = CaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);


    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginForm form) throws IOException {
        boolean captcha = CaptchaService.validate(form.getUuid(), form.getCaptcha());
        if (!captcha) {
            return R.error("验证码不正确");
        }

        //用户信息
        UserEntity user = UserService.queryByUserName(form.getUsername());

        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }

        //账号锁定
        if (user.getStatus() == 0) {
            return R.error("账号已被锁定,请联系管理员");
        }

        //生成token，并保存到数据库
        R r = UserTokenService.createToken(user.getUserId());
        return r;
    }


    /**
     * 退出
     */
    @PostMapping("/sys/logout")
    public R logout() {
        UserTokenService.logout(getUserId());
        return R.ok();
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R Register(@RequestBody RegisterForm registerForm)throws IOException{

        UserEntity user = new UserEntity();
        user.setUsername(registerForm.getUsername());
        user.setPassword(registerForm.getPassword());
        user.setAddress(registerForm.getAddress());
        user.setEmail(registerForm.getEmail());
        user.setMobile(registerForm.getMobile());
        UserService.saveUser(user);
        return R.ok();
    }

}

